package com.web.admin.web.cms.adv;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.cms.ad.facade.AdvFacade;
import com.web.cms.ad.facade.AdvPosFacade;
import com.web.cms.ad.message.AdvRequest;
import com.web.cms.ad.message.AdvResponse;
import com.web.cms.doc.facade.WebFacade;
import com.web.cms.doc.message.WebResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller("advAdvController")
@Http(url = "/cms/adv")
public class AdvController
{
	@Autowired
	private AdvFacade advFacade;
	@Autowired
	private AdvPosFacade advPosFacade;
	@Autowired
	private WebFacade webFacade;
	
	private DictParser parser = DictParser.getInstance();
	
	public AdvController()
	{
	}
	
	@Http(url = "/adv_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		AdvRequest advRequest = request.getQueryObject(AdvRequest.class);
		List<WebResponse> webList = webFacade.listAll();
		if (advRequest.getWebId() == null && !webList.isEmpty())
		{
			advRequest.setWebId(webList.get(0).getId());
		}
		return View.createBuilder().setView("/cms/adv/adv_list")
				.put("model", advRequest)
				.put("page", advFacade.listPage(advRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.put("map", parser.getStringMap())
				.put("webList", webList)
				.put("posList", advPosFacade.listAll(advRequest.getWebId()))
				.build();
	}
	
	@Http(url = "/adv_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		AdvRequest advRequest = request.getQueryObject(AdvRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		return View.createBuilder().setView("/cms/adv/adv_list_grid")
				.put("page", advFacade.listPage(advRequest, start, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/adv_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		AdvRequest advRequest = request.getQueryObject(AdvRequest.class);
		WebResponse webResponse =  null;
		AdvResponse response = null;
		if (advRequest.getId() != null)
		{
			response = advFacade.get(advRequest.getId());
		}
		if (response == null)
		{
			response = new AdvResponse();
			response.setWebId(advRequest.getWebId());
		}
		if (response.getWebId() != null)
		{
			webResponse = webFacade.get(response.getWebId());
		}
		return View.createBuilder().setView("/cms/adv/adv_get")
				.put("web", webResponse)
				.put("model", response)
				.put("posList", advPosFacade.listAll(response.getWebId()))
				.put("map", parser.getStringMap())
				.build();
	}
	
	@Http(url = "/adv_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		AdvRequest advRequest = request.getPostObject(AdvRequest.class);
		AdvResponse response = null;
		if (advRequest.getId() == null)
		{
			ValidationUtil.valid(advRequest, SaveModel.class);
			response = advFacade.add(advRequest);
		}
		else
		{
			ValidationUtil.valid(advRequest, UpdateModel.class);
			response = advFacade.update(advRequest);
		}
	}
	
	@Http(url = "/adv_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
	{
		advFacade.refresh();
	}

}
